﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL;
using dhSystemBLL.orderData;
using baseSet.importExport;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using dhSystemBLL.basicData;
using baseSet;
using dhDataAcess.baseBLL;
using dhsystem.produceForm;
using dhsystem.Main;
using dhsystem.produceForm.Stock;
using dhSystemBLL.produceBLL;
using FastReport;
using dhDataAcess.sysDAL;

namespace dhsystem.orderForm
{
    public partial class FrmorderMasterAdd : Form
    {

        orderProduceListBLL orderProduceList = new orderProduceListBLL();
        orderDataSet.orderDetailDataTable orderDetailTable = new orderDataSet.orderDetailDataTable();
        orderMasterBLL orderMaster = new orderMasterBLL();
        orderMasterModel orderMasterRow = new orderMasterModel();
        orderDetailBLL orderDetail = new orderDetailBLL();
        orderDetailModel orderDetailRow = new orderDetailModel();
        custListBLL custList = new custListBLL();
        userListBLL userList = new userListBLL();
        custAddListBLL custAdd = new custAddListBLL();
        workerListBLL workerList = new workerListBLL();
        basListBLL basList = new basListBLL();
        productUnitBLL unit = new productUnitBLL();
        orderDetailUpdateBLL orderDetailUpdate = new orderDetailUpdateBLL();
        BindingSource BindingSourceorderMasterAdd;
        string state;
        string orderDetailNo;
        public FrmorderMasterAdd(BindingSource BindingSourceorderMaster, string _state)
        {
            InitializeComponent();
            state = _state;
            BindingSourceorderMasterAdd = BindingSourceorderMaster;
        }
      
        //打开窗口时绑定数据
        private void FrmorderMasterAdd_Load(object sender, EventArgs e)
        {
            
           
            
            bindingSourcecustList.DataSource = custList.getcustListAll();
            comboBoxcustNum .DataSource  = bindingSourcecustList ;
            comboBoxcustNum.DisplayMember = "custName";
            comboBoxcustNum.ValueMember = "custNum";
           
            bindingSourceuserList.DataSource = workerList.getworkerListAll();
            comboBoxuserNum.DataSource = bindingSourceuserList;
            comboBoxuserNum.DisplayMember = "name";
            comboBoxuserNum.ValueMember = "workerNum";

            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";


            if (state == "新增")
            {
                textorderNo.Text = "O" +GlobalInfo .UserName + GlobalInfo.Date + "-" + basicSql.getOrderSerail("订单序列号");
                bindingSourceorderDetail.DataSource = orderDetail.getorderDetailByorderNo (textorderNo.Text );
                dGridDetail.DataSource = bindingSourceorderDetail;
                textkjf.Text = "0";
                textscf.Text = "0";
               textaccount.Text = "0";
               bindingSourceorderDetail.AddNew();
            }
            if (state == "编辑")
            {
                textorderNo.DataBindings.Add("text", BindingSourceorderMasterAdd, "orderNo");
                comboBoxcustNum.DataBindings.Add("SelectedValue", BindingSourceorderMasterAdd, "custNum");
                dateTimeorderDate.DataBindings.Add("Value", BindingSourceorderMasterAdd, "orderDate");
                dateTimereceiveDate.DataBindings.Add("Value", BindingSourceorderMasterAdd, "receiveDate");
                textcustPoNo.DataBindings.Add("text", BindingSourceorderMasterAdd, "custPoNo");
                comboBoxbizhong.DataBindings.Add("text", BindingSourceorderMasterAdd, "billNo");
                textorderMark.DataBindings.Add("text", BindingSourceorderMasterAdd, "orderMark");
                comboBoxuserNum.DataBindings.Add("SelectedValue", BindingSourceorderMasterAdd, "userNum");
                comboBoxworkername.DataBindings.Add("SelectedValue", BindingSourceorderMasterAdd, "workername");
               
                textkjf.DataBindings.Add("text", BindingSourceorderMasterAdd, "kjf");
                textscf.DataBindings.Add("text", BindingSourceorderMasterAdd, "scf");
                textaccount.DataBindings.Add("text", BindingSourceorderMasterAdd, "account");
                textparkMark.DataBindings.Add("text", BindingSourceorderMasterAdd, "parkMark");
                textmark.DataBindings.Add("text", BindingSourceorderMasterAdd, "mark");
                comboBoxaddress.DataBindings.Add("SelectedValue", BindingSourceorderMasterAdd, "address");

                bindingSourceorderDetail.DataSource = orderDetail.getorderDetailByorderNo (textorderNo.Text);
                dGridDetail.DataSource = bindingSourceorderDetail;
               
                if (basicSql.getOrderStatus(textorderNo.Text) != "新建")
                {
                    dGridDetail.Columns[7].ReadOnly = true;
                }
            }
               
              //  dGridDetail.AllowUserToAddRows = true;
              // dGridDetail.Columns[2].ReadOnly = true;
            dGridDetail.Columns["ptype"].ReadOnly = true;
                dGridDetail.Columns[3].ReadOnly = true;
                dGridDetail.Columns[6].ReadOnly = true;
                dGridDetail.Columns[11].ReadOnly = true;
                //dGridDetail.Columns[14].ReadOnly = true;
                dGridDetail.Columns["account"].ReadOnly  = true;
                dGridDetail.Columns["orderDetailNo"].ReadOnly = true;
                this.KeyPreview = true;
                textcustPoNo.Focus();
                dGridDetail.EditMode = DataGridViewEditMode.EditOnEnter;

                dGridDetail.AllowUserToAddRows = false;
             
                DcolumnSet.colSet(moduleInfo.orderDetail, dGridDetail);
        }
        int i = 0;
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
        
                try
                {
                    DialogResult result = MessageBox.Show("是否保存订单表表信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        if (i == 0 && state == "新增")
                        {
                            state = "新增";
                        }
                        else
                        {
                            state = "编辑";
                        }
                        i++;
                        dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable  ptable = (dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable )bindingSourceorderDetail .DataSource ;
                       // orderDataSet.orderMasterDataTable orderNo = orderMaster.getorderMasterAllByorderNo(textorderNo.Text);
                        orderMasterRow.orderNo = textorderNo.Text;
                        orderMasterRow .custNum =comboBoxcustNum .SelectedValue .ToString ();
                        orderMasterRow.orderDate = dateTimeorderDate.Value ;
                        orderMasterRow.receiveDate = dateTimereceiveDate.Value;
                        orderMasterRow.custPoNo = textcustPoNo.Text;
                        orderMasterRow.billNo = comboBoxbizhong.Text ;
                        orderMasterRow .orderMark =textmark .Text ;
                        orderMasterRow .userNum =comboBoxuserNum.SelectedValue .ToString ();
                        orderMasterRow.workername =comboBoxworkername .SelectedValue .ToString ();
                        orderMasterRow .kjf =Convert.ToDecimal( textkjf .Text) ;
                        orderMasterRow .scf =Convert.ToDecimal(textscf .Text );
                        orderMasterRow .account =Convert.ToDecimal(textaccount .Text) ;
                        orderMasterRow .billNo =comboBoxbizhong .Text ;
                        orderMasterRow .parkMark =textparkMark .Text ;
                        orderMasterRow.orderStatus = "新建";
                        orderMasterRow .address =comboBoxaddress .SelectedValue .ToString ();
                        orderMasterRow.createDate = DateTime.Now;
                        orderMasterRow.createUser = GlobalInfo.UserName;
                        orderMasterRow.updateDate = DateTime.Now;
                        orderMasterRow.updateUser = GlobalInfo.UserName;
                        orderMasterRow .mark =textmark .Text ;
                        orderMaster.orderMasterInsert(orderMasterRow ,ptable ,state,GlobalInfo .UserName,textorderNo .Text,GlobalInfo .Date );
                        ptable.AcceptChanges();
                        bindingSourceorderMaster.DataSource =orderMaster .getorderMasterAll ();
                        MessageBox.Show("保存成功");
                    }
                 }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
      
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //添加子件
        private void buttonDetail_Click(object sender, EventArgs e)
        {
            // textorderNo.Text = "O" + GlobalInfo.Date + "-" + baseSet.basicSql.getOrderSerail("订单序列号");
            FrmorderDetailAdd orderDetailAdd = new FrmorderDetailAdd(bindingSourceorderDetail, "新增", textorderNo.Text);
            orderDetailAdd.ShowDialog();
        }
        //编辑子件
        private void button1_Click(object sender, EventArgs e)
        {
            FrmorderDetailAdd orderDetailAdd = new FrmorderDetailAdd(bindingSourceorderDetail , "编辑", textorderNo.Text);
            orderDetailAdd.ShowDialog();
        }
        //删除子件
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string orderNo= dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[1].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除订单：" + orderNo + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dGridDetail.CurrentCell.Value = "12345544";
        }
        //客户编号选中按钮
        private void buttselectcustNum_Click(object sender, EventArgs e)
        {
            string field;
            field = "custNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.custList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxcustNum.SelectedValue = productselect.pname;
            }
        }
        //跟单员编号选中按钮
        private void buttuserselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "workerNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxuserNum.SelectedValue = productselect.pname;
            }
        }
        //业务员编号选择按钮
        private void buttworkerselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "workerNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxworkername.SelectedValue = productselect.pname;
            }
        }
        //双击原材料所在行单元格事件
        /// <summary>  
        /// 设置编辑状态下按回车键，跳到指定单元格.  
        /// </summary>  
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            string field;
            field = "pname";
            //string field2;
            //field2 = "colorNum";

            string field3;
            field3 = "unitEn";
            if (keyData == Keys.Enter)    //监听回车事件   
            {
                if (this.dGridDetail.IsCurrentCellInEditMode)   //如果当前单元格处于编辑模式   
                {
                    if (this.dGridDetail.CurrentCell.ColumnIndex == 2)
                    {
                        Frmprodutselect select = new Frmprodutselect(moduleInfo.productList , field);
                        DialogResult result = select.ShowDialog();
                        if (result == DialogResult.Yes)
                        {
                            dGridDetail.CurrentRow.Cells["pname"].Value = select.pname;
                        }
                    }

                    if (this.dGridDetail.CurrentCell.ColumnIndex == 5)
                    {
                        Frmprodutselect select = new Frmprodutselect(moduleInfo.productUnit, field3);
                        DialogResult result = select.ShowDialog();
                        if (result == DialogResult.Yes)
                        {

                            dGridDetail.CurrentRow.Cells["unit"].Value = select.pname;
                        }
                    }
                    if (this.dGridDetail.CurrentCell.ColumnIndex == dGridDetail.Columns["mark"].Index)
                    {
                        try
                        {

                            //这里做一个为空的检测
                            //
                            if (dGridDetail.CurrentRow.Cells["unit"].Value.ToString() == "")
                            {
                                MessageBox.Show("单位名称不能为空");
                                return true;
                            }
                            if (dGridDetail.Rows.Count == dGridDetail.CurrentRow.Index+1)
                            {
                                bindingSourceorderDetail.AddNew();
                            }
                            else
                            {
                                dGridDetail.CurrentCell = dGridDetail.Rows[dGridDetail.CurrentRow.Index + 1].Cells["orderDetailNo"];
                                return true; 
                            }
                        }
                        catch (ConstraintException )
                        {
                            MessageBox.Show("漂染单号重复");
                        }
                        return true;
                        
                        //
                    }

                    if (this.dGridDetail.CurrentCell.ColumnIndex != dGridDetail.Columns["mark"].Index)
                    {
                        SendKeys.Send("{Tab}");
                        return true;
                    }
                    else
                    {
                        SendKeys.Send("{LEFT}");
                        //SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}");
                        //SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}");SendKeys.Send("{LEFT}");
                    }
                }
                else
                {
                   
                    SendKeys.Send("{Tab}");
                }


            }

            if (keyData == Keys.F3)
            {

                int cRowIndex = dGridDetail.CurrentCell.RowIndex;
                int cCellIndex = dGridDetail.CurrentCell.ColumnIndex;
                
                dGridDetail.CurrentCell.Value = dGridDetail.Rows[cRowIndex - 1].Cells[cCellIndex].Value;
                if (cCellIndex == 2 || cCellIndex == 0 )
                {
                    dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells[cCellIndex + 3];
                    dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells[cCellIndex];
                }
                else
                {
                   
                    dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells[cCellIndex +1];
                    dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells[cCellIndex];
                }
            }

            //继续原来base.ProcessCmdKey中的处理   
            return base.ProcessCmdKey(ref msg, keyData);

        }
        //菜单栏删除按钮
        private void toolDel_Click(object sender, EventArgs e)
        {

            try
            {
                if (basicSql.getOrderStatus(textorderNo.Text) == "审核")
                {
                    MessageBox.Show("该订单已审核，不能删除！如需取消该订单明细，请选中需要取消的订单明细鼠标单击右键选择取消订单明细");
                }
                else
                {
                    string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                    DialogResult result = MessageBox.Show("是否删除明细！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //菜单栏保存按钮
        private void toolSave_Click(object sender, EventArgs e)
        {
            if(state =="新增")
            {
            if (comboBoxcustNum.Text == "")
            {
                MessageBox.Show("请选择客户编号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存订单表信息！", "提示信息", MessageBoxButtons.YesNo);
                    
                    if (result == DialogResult.Yes)
                    {

                        dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable ptable = (dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable)bindingSourceorderDetail.DataSource;
                        orderMasterRow.orderNo = textorderNo.Text;
                        orderMasterRow.custNum = comboBoxcustNum.SelectedValue.ToString();
                        orderMasterRow.orderDate = dateTimeorderDate.Value;
                        orderMasterRow.receiveDate = dateTimereceiveDate.Value;
                        orderMasterRow.custPoNo = textcustPoNo.Text;
                        orderMasterRow.billNo = comboBoxbizhong.Text;
                        orderMasterRow.orderMark = textmark.Text;
                        orderMasterRow.userNum = comboBoxuserNum.Text;
                        orderMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        if (textkjf.Text != "")
                        {
                            orderMasterRow.kjf = Convert.ToDecimal(textkjf.Text);
                        }
                        else { orderMasterRow.kjf = 0; }
                        if (textscf.Text != "")
                        {
                            orderMasterRow.scf = Convert.ToDecimal(textscf.Text);
                        }
                        else { orderMasterRow.scf = 0; }
                        orderMasterRow.account = Convert.ToDecimal(textaccount.Text);
                       // orderMasterRow.billNo = "";
                        orderMasterRow.orderMark = textorderMark.Text;
                        orderMasterRow.parkMark = textparkMark.Text;
                        orderMasterRow.orderStatus = "新建";
                        if (comboBoxaddress.SelectedValue == null) orderMasterRow.address = "";
                        else orderMasterRow.address = comboBoxaddress.SelectedValue.ToString();
                        orderMasterRow.createDate = DateTime.Now;
                        orderMasterRow.createUser = GlobalInfo.UserName;
                        orderMasterRow.updateDate = DateTime.Now;
                        orderMasterRow.updateUser = GlobalInfo.UserName;
                        orderMasterRow.mark = textmark.Text;
                        //if (state == "新增")
                        //{
                            bindingSourceorderDetail.AddNew();
                        //}
                        orderMaster.orderMasterInsert(orderMasterRow, ptable, state, GlobalInfo.UserName, textorderNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourceorderMaster.DataSource = orderMaster.getorderMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException )
                {
                    MessageBox.Show("输入的开机费、金额格式不正确");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            }
            if (state == "编辑")
            {
               // string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
                if (basicSql.getOrderStatus(textorderNo .Text ) == "新建")
                {
                    if (comboBoxcustNum.Text == "")
                    {
                        MessageBox.Show("请选择客户编号");
                    }
                    else
                    {
                        try
                        {
                            DialogResult result = MessageBox.Show("是否保存订单表信息！", "提示信息", MessageBoxButtons.YesNo);

                            if (result == DialogResult.Yes)
                            {

                                dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable ptable = (dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable)bindingSourceorderDetail.DataSource;
                                orderMasterRow.orderNo = textorderNo.Text;
                                orderMasterRow.custNum = comboBoxcustNum.SelectedValue.ToString();
                                orderMasterRow.orderDate = dateTimeorderDate.Value;
                                orderMasterRow.receiveDate = dateTimeorderDate.Value;
                                orderMasterRow.custPoNo = textcustPoNo.Text;
                                // orderMasterRow.billNo = texbillNo.Text;
                                orderMasterRow.orderMark = textmark.Text;
                                orderMasterRow.userNum = comboBoxuserNum.Text;
                                orderMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                                orderMasterRow.kjf = Convert.ToDecimal(textkjf.Text);
                                orderMasterRow.scf = Convert.ToDecimal(textscf.Text);
                                orderMasterRow.account = Convert.ToDecimal(textaccount.Text);
                                orderMasterRow.billNo = "";
                                orderMasterRow.orderMark = textorderMark.Text;
                                orderMasterRow.parkMark = textparkMark.Text;
                                orderMasterRow.orderStatus = "新建";
                                if (comboBoxaddress.SelectedValue == null) orderMasterRow.address = "";
                                else orderMasterRow.address = comboBoxaddress.SelectedValue.ToString();
                                orderMasterRow.createDate = DateTime.Now;
                                orderMasterRow.createUser = GlobalInfo.UserName;
                                orderMasterRow.updateDate = DateTime.Now;
                                orderMasterRow.updateUser = GlobalInfo.UserName;
                                orderMasterRow.mark = textmark.Text;
                                //if (state == "新增")
                                //{
                                bindingSourceorderDetail.AddNew();
                                //}
                                orderMaster.orderMasterInsert(orderMasterRow, ptable, state, GlobalInfo.UserName, textorderNo.Text, GlobalInfo.Date);
                                ptable.AcceptChanges();
                                bindingSourceorderMaster.DataSource = orderMaster.getorderMasterAll();
                                MessageBox.Show("保存成功");
                            }
                        }
                        catch (FormatException)
                        {
                            MessageBox.Show("输入的开机费、金额格式不正确");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                }

                else
                {
                    MessageBox.Show("该订单已审核，不能进行编辑");
                }
            }
        }
        //状态栏退出按钮
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 复制选中的内容ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (dGridDetail.Rows.Count != 1)
                {
                    orderDataSet.pbomDetailDataTable detailTable = (orderDataSet.pbomDetailDataTable)bindingSourceorderDetail.DataSource;
                    orderDataSet.pbomDetailRow insertRow = detailTable.NewpbomDetailRow();
                    int recordID = Convert.ToInt32(dGridDetail.CurrentRow.Cells["recordID"].Value.ToString());
                    orderDataSet.pbomDetailRow crreentRow = detailTable.FindByrecordid(recordID);
                    insertRow.wb = crreentRow.wb;
                    insertRow.matNum = crreentRow.matNum;
                    insertRow.pt = crreentRow.pt;
                    insertRow.pz = crreentRow.pz;
                    insertRow.ts = crreentRow.ts;
                    insertRow.blen = crreentRow.blen;
                    insertRow.pweight = crreentRow.pweight;
                    insertRow.alen = crreentRow.alen;
                    insertRow.aweight = crreentRow.aweight;
                    insertRow.mark = crreentRow.mark;
                    detailTable.AddpbomDetailRow(insertRow);
                    bindingSourceorderDetail.MoveLast();
                    bindingSourceorderDetail.EndEdit();
                }
                else
                {
                    MessageBox.Show("不存在需要复制的行");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请选择要复制的行");
            }
        }

        private void 复制插入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (dGridDetail.Rows.Count != 1)
                {
                    orderDataSet.pbomDetailDataTable detailTable = (orderDataSet.pbomDetailDataTable)bindingSourceorderDetail.DataSource;
                    orderDataSet.pbomDetailRow insertRow = detailTable.NewpbomDetailRow();
                    int recordID = Convert.ToInt32(dGridDetail.CurrentRow.Cells["recordID"].Value.ToString());
                    orderDataSet.pbomDetailRow crreentRow = detailTable.FindByrecordid(recordID);
                    insertRow.wb = crreentRow.wb;
                    insertRow.matNum = crreentRow.matNum;
                    insertRow.pt = crreentRow.pt;
                    insertRow.pz = crreentRow.pz;
                    insertRow.ts = crreentRow.ts;
                    insertRow.blen = crreentRow.blen;
                    insertRow.pweight = crreentRow.pweight;
                    insertRow.alen = crreentRow.alen;
                    insertRow.aweight = crreentRow.aweight;
                    insertRow.mark = crreentRow.mark;
                    detailTable.Rows.InsertAt((DataRow)insertRow, dGridDetail.CurrentRow.Index + 1);
                    bindingSourceorderDetail.MoveNext();
                    bindingSourceorderDetail.EndEdit();
                }
                else
                {
                    MessageBox.Show("不存在需要复制的行");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请选择要插入的行的位置");
            }
        }

        private void 插入空行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (dGridDetail.Rows.Count != 1)
                {
                    orderDataSet.pbomDetailDataTable detailTable = (orderDataSet.pbomDetailDataTable)bindingSourceorderDetail.DataSource;
                    orderDataSet.pbomDetailRow insertRow = detailTable.NewpbomDetailRow();
                    detailTable.Rows.InsertAt((DataRow)insertRow, dGridDetail.CurrentRow.Index + 1);
                    bindingSourceorderDetail.MoveNext();
                }
                else
                {
                    MessageBox.Show("不存在需要插入的行");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请选需要插入空行的位置");
            }
        }

        private void 复制单元格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int cRowIndex = dGridDetail.CurrentCell.RowIndex;
            int cCellIndex = dGridDetail.CurrentCell.ColumnIndex;
            dGridDetail.CurrentCell.Value = dGridDetail.Rows[cRowIndex - 1].Cells[cCellIndex].Value;
            if (cCellIndex == 2 || cCellIndex == 0)
            {
                dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells[cCellIndex + 3];
                dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells[cCellIndex];
            }
            else
            {
                dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells[cCellIndex - 1];
                dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells[cCellIndex];
            }
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //菜单栏新增按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            textcustPoNo .Text  = "";
           // texbillNo.Text = "";
            dateTimeorderDate.Value = DateTime.Now;
            textorderMark.Text = "";
            textkjf.Text = "";
            textscf.Text = "";
            textaccount.Text = "";
            textparkMark.Text = "";
            textmark.Text = "";
            textorderNo.Text = "O" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("订单序列号");
            bindingSourceorderDetail.DataSource = orderDetail.getorderDetailByorderNo (textorderNo.Text);
            dGridDetail.DataSource = bindingSourceorderDetail;

            
            textcustPoNo.Focus();
            bindingSourcecustList.DataSource = custList.getcustListAll();
            comboBoxcustNum.DataSource = bindingSourcecustList;
            comboBoxcustNum.DisplayMember = "custName";
            comboBoxcustNum.ValueMember = "custNum";
            bindingSourceuserList.DataSource = workerList.getworkerListAll();
            comboBoxuserNum.DataSource = bindingSourceuserList;
            comboBoxuserNum.DisplayMember = "name";
            comboBoxuserNum.ValueMember = "workerNum";

            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";

            DcolumnSet.colSet(moduleInfo.orderDetail, dGridDetail);
            dGridDetail.AllowUserToAddRows = true;
            dGridDetail.Columns[3].ReadOnly = true;
            this.KeyPreview = true;

            
        }

        private void dGridDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
         
            decimal  decAmountTotal=0 ;
            if (dGridDetail.Rows.Count > 0 && e.RowIndex >= 0)
            {
                DataGridViewRow row = dGridDetail.Rows[e.RowIndex];
                if (row.Cells[e.ColumnIndex].OwningColumn.Name == "pname" || row.Cells[e.ColumnIndex].OwningColumn.Name == "qty" || row.Cells[e.ColumnIndex].OwningColumn.Name == "price"
                    ) 
                {
                     
                    try
                    {
                        row.Cells["proType"].Value = basicSql.getproTypebyproductList (row.Cells["pname"].Value.ToString());
                        row.Cells["ptype"].Value = basicSql.getptypebyproductList(row.Cells["pname"].Value.ToString());
                        string orderDetialNo = row.Cells["orderDetailNo"].Value.ToString();
                        if (string.IsNullOrEmpty(orderDetialNo))
                        {
                            //MessageBox.Show("空的漂染单号" + orderDetialNo);
                            string p=basicSql.getOrderSerail("漂染单号");
                            if (p.Length == 1)
                            {
                                p = "000" + p;
                            }
                            if (p.Length ==2)
                            {
                                p = "00" + p;
                            }
                            if (p.Length == 3)
                            {
                                p = "0" + p;
                            }
                            row.Cells["orderDetailNo"].Value = "P" + GlobalInfo.Date + p ;
                            string orderSerial = basicSql.getOrderSerail("漂染单号");
                            basList.basListstretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "漂染单号");
                        }
                        string  account;
                        //account = Convert.ToDecimal(row.Cells["qty"].Value) * Convert.ToDecimal(row.Cells["price"].Value);
                        if (row.Cells["price"].Value.ToString() != "" && row.Cells["qty"].Value.ToString() != "")
                        {
                            account =( Convert.ToDecimal(row.Cells["qty"].Value) * Convert.ToDecimal(row.Cells["price"].Value)).ToString ("#0.0000");
                            row.Cells["account"].Value = account;
                            row.Cells["account"].ReadOnly = true;
                            
                            //数量或单价更改后,重新计算 数量合计和金额合计

                            for (int i = 0; i < dGridDetail.Rows.Count; i++)
                            {
                                decAmountTotal += Convert.ToDecimal(dGridDetail.Rows[i].Cells["account"].Value);
                               // dGridDetail.Rows[i].Cells["ptype"].Value = basicSql.getptypebyproductList(row.Cells["pname"].Value.ToString());

                            }
                            textaccount.Text = decAmountTotal.ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString ());
                    }
                }
            }
        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void 复制上一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 制作生产通知单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
                string unit = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["unit"].Value.ToString();
                if (basicSql.getOrderStatus(orderNo) == "审核")
                {
                    orderDetailNo = dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString();
                    FrmorderProduceListAdd orderProduceListAdd = new FrmorderProduceListAdd(bindingSourceorderProduceList, "新增", orderDetailNo,  unit);
                    orderProduceListAdd.ShowDialog();
                }
                else
                {
                    MessageBox.Show("该订单未审核，不能制作生产通知单");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        //生产单刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
            bindingSourceorderProduceList.DataSource = orderProduceList.getVByproduceNo(textorderNo.Text);
            dGridorderProduceList.DataSource = bindingSourceorderProduceList;
            DcolumnSet.colSet(moduleInfo.V_ordeProduceList , dGridorderProduceList);
            toolStripStatusLabel1.Text = "数据总条数:" + dGridorderProduceList.RowCount.ToString() + "条";
        }
        //生产通知单查询按钮
        private void buttonQuery_Click(object sender, EventArgs e)
        {
           
            //bindingSourceorderProduceList.DataSource = orderProduceList.getVorderProduceListByproduceNo ("%" + textorderDetailNoQuery.Text + "%");
            //dGridorderProduceList.DataSource = bindingSourceorderProduceList;
            //DcolumnSet.colSet(moduleInfo.V_ordeProduceList , dGridorderProduceList);
            //toolStripStatusLabel1.Text = "数据总条数:" + dGridorderProduceList.RowCount.ToString() + "条";
        }

        private void 编辑生产通知单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
                string unit = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["unit"].Value.ToString();
                if (basicSql.getOrderStatus(orderNo) == "审核")
                {
                    orderDetailNo = dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString();
                    FrmorderProduceListAdd orderProduceListAdd = new FrmorderProduceListAdd(bindingSourceorderProduceList, "编辑", orderDetailNo, unit);
                    orderProduceListAdd.ShowDialog();
                }
                else
                {
                    MessageBox.Show("该订单未审核，不能编辑生产通知单");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        private void 取消订单明细ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string orderDetailNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderDetailNo"].Value.ToString();
                 string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
              //   MessageBox.Show(basicSql.getOrderStatus(orderNo));
                 if (basicSql.getOrderStatus(orderNo) == "审核")
                 {
                     basicSql.getDetailStatusUpdate(orderDetailNo);
                     MessageBox.Show("取消订单明细成功");
                 }
                 else
                 {
                     MessageBox.Show("订单没有审核，可直接删除，无需取消");
                 }
            }
            catch (Exception ex)
            {
                throw new Exception (ex.ToString());
            }
        }
        //生产通知单
        private void tabPage2_Click(object sender, EventArgs e)
        {
            bindingSourceorderProduceList.DataSource = orderProduceList.getVorderProduceList();
            dGridorderProduceList.DataSource = bindingSourceorderProduceList;
            DcolumnSet.colSet(moduleInfo.orderProduceList, dGridorderProduceList);
            toolStripStatusLabel1.Text = "数据总条数:" + dGridorderProduceList.RowCount.ToString() + "条";
          
        }

        private void 订单数量变更ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
                if (basicSql.getOrderStatus(orderNo) != "新建")
                {
                   
                    orderDetailNo = dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString();
                   
                  string  qty =dGridDetail.CurrentRow.Cells["qty"].Value.ToString ();
                 
                    FrmorderDetailUpdateAdd orderDetailUpdateAdd = new FrmorderDetailUpdateAdd(bindingSourceorderDetailUpdate, "新增", orderDetailNo,orderNo ,qty );
                    DialogResult result = orderDetailUpdateAdd.ShowDialog();
                    if (result == DialogResult.Yes)
                    {
                        dGridDetail.CurrentRow.Cells["qty"].Value = orderDetailUpdateAdd.retqty;
                        basicSql.getorderDetailqtyUpdate(orderDetailNo, orderDetailUpdateAdd.retqty);
                    }
                    
                }
                else
                {
                    MessageBox.Show("该订单未审核，无需做进行数量变更");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        //打开变更明细表记录
        private void tabPage3_Click(object sender, EventArgs e)
        {
            bindingSourceorderDetailUpdate.DataSource = orderDetailUpdate.getorderDetailUpdateAllByorderNo(textorderNo.Text);
            dGridorderDetailUpdate.DataSource = bindingSourceorderDetailUpdate;
        }

        //数量变更单
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindingSourceorderDetailUpdate.DataSource = orderDetailUpdate.getorderDetailUpdateAllByorderNo(textorderNo.Text);
            dGridorderDetailUpdate.DataSource = bindingSourceorderDetailUpdate;
            DcolumnSet.colSet(moduleInfo.orderDetailUpdate, dGridorderDetailUpdate);
        }

        private void 查看生产通知单ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            tabControl1.SelectedIndex = 2;
            textorderDetailNoQuery.Text = dGridDetail.CurrentRow.Cells["orderNo"].Value.ToString();
            buttonQuery.PerformClick();
        }
     
        //客户编号发生改变时
        private void comboBoxcustNum_SelectedValueChanged(object sender, EventArgs e)
        {
          
            bindingSourcecustListadd.DataSource = custAdd.getcustAddAll(comboBoxcustNum.SelectedValue.ToString());
            comboBoxaddress.DataSource = bindingSourcecustListadd;
            comboBoxaddress.DisplayMember = "addres";
            comboBoxaddress.ValueMember = "addres";
        }

        private void dGridDetail_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            DataGridViewRow row=dGridDetail.Rows[e.RowIndex];
            if (row.Cells ["orderDetailNo"].Value.ToString()=="")
            {
                DialogResult result = MessageBox.Show("漂染单号不能为空,是否删除该行","",MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes) e.Cancel = false;
                else e.Cancel = true;
            }

            if(row.Cells["qty"].Value.GetType()!=typeof(decimal))
            {
                MessageBox.Show("请输入正确的数量");
                e.Cancel = true;
            }

            if (row.Cells["price"].Value.GetType() != typeof(decimal))
            {
                MessageBox.Show("请输入正确的单价");
                e.Cancel = true;
            }
            if (row.Cells["pname"].Value.ToString()=="")
            {
                MessageBox.Show("产品名称不能为空");
                e.Cancel = true;
            }
            if (row.Cells["unit"].Value.ToString() == "")
            {
                MessageBox.Show("单位不能为空");
                e.Cancel = true;
            }
           // e.Cancel = true;
        }

        private void bindingSourceorderDetail_AddingNew(object sender, AddingNewEventArgs e)
        {
            try
            {
                dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells["orderDetailNo"];
                //MessageBox.Show("漂染单号不能为空");
                //这里做一个为空检测
            }
            catch { }
        }

        private void bindingSourceorderDetail_PositionChanged(object sender, EventArgs e)
        {

        }
        //添加明细
        private void button3_Click_1(object sender, EventArgs e)
        {
            FrmorderDetailAdd orderDetailAdd = new FrmorderDetailAdd(bindingSourceorderDetail, "新增", textorderNo.Text);
            orderDetailAdd.ShowDialog();
        }
        //编辑明细
        private void button6_Click(object sender, EventArgs e)
        {
            FrmorderDetailAdd orderDetailAdd = new FrmorderDetailAdd(bindingSourceorderDetail, "编辑", textorderNo.Text);
            orderDetailAdd.ShowDialog();
        }
        //删除明细
        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                if (basicSql.getOrderStatus(textorderNo .Text ) == "审核")
                {
                    MessageBox.Show("该订单已审核，不能删除！如需取消该订单明细，请选中需要取消的订单明细鼠标单击右键选择取消订单明细");
                }
                else
                {
                    string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                    DialogResult result = MessageBox.Show("是否删除明细！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //双击原材料所在行单元格事件


        private void toolStripButton4_Click(object sender, EventArgs e)
        {

        }
        //编辑生产通知单
        private void tooledit_Click(object sender, EventArgs e)
        {
            try
            {
                string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
                string unit = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["unit"].Value.ToString();
                if (basicSql.getOrderStatus(orderNo) == "审核")
                {
                    orderDetailNo = dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString();
                    FrmorderProduceListAdd orderProduceListAdd = new FrmorderProduceListAdd(bindingSourceorderProduceList, "编辑", orderDetailNo, unit);
                    orderProduceListAdd.ShowDialog();
                }
                else
                {
                    MessageBox.Show("该订单未审核，不能编辑生产通知单");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        //删除生产通知单
        private void toolStripDel_Click(object sender, EventArgs e)
        {
            string produceNo = dGridorderProduceList.CurrentRow.Cells["produceNo"].Value.ToString();
            string orderDetailNo = dGridorderProduceList.CurrentRow.Cells["orderDetailNo"].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除生产通知单：" + produceNo + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceorderProduceList.DataSource = orderProduceList.orderProduceListDel(produceNo);
                    dGridorderProduceList.DataSource = bindingSourceorderProduceList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
           // bindingSourceorderProduceList.DataSource = orderProduceList.getorderProduceListAll();
        }
        //生产通知单退出
        private void toolStripexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //打印生产通知单
        private void toolprinte_Click(object sender, EventArgs e)
        {
            //List<string> list = new List<string>();
            //foreach (DataGridViewRow row in dGridorderProduceList.SelectedRows)
            //{
            //    list.Add(row.Cells["produceNo"].Value.ToString());
            //}
            //basicSql.getproduceNo(list);
            //Report r = new Report();
            //DataTable V_ordeProduceList = new DataTable();
            //V_ordeProduceList.Load(basicSql.getproduceNo(list));
            //DataSet ds = new DataSet();
            //ds.Tables.Add(V_ordeProduceList);
            ////  ds.Tables.Add(orderProduceList.getproduceByproduceNo(dGridorderProduceList.CurrentRow.Cells["produceNo"].Value.ToString()));
            //r.Load(@"e:\东华软件项目\报表\orderproduce.frx");
            //r.RegisterData(ds);
            //r.Design();
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            //生产通知单
            if (e.TabPage == tabPage4)
            {
                bindingSourceorderProduceList.DataSource = orderProduceList.getVByproduceNo ( textorderNo .Text);
                dGridorderProduceList.DataSource = bindingSourceorderProduceList;
                DcolumnSet.colSet(moduleInfo.V_ordeProduceList , dGridorderProduceList);
                toolStripStatusLabel1.Text = "数据总条数:" + dGridorderProduceList.RowCount.ToString() + "条";
            }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //删除变更通知单
        private void toolStripButton7_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            try
            {
                string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
                if (basicSql.getOrderStatus(orderNo) == "审核")
                {

                    orderDetailNo = dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString();

                    string qty = dGridDetail.CurrentRow.Cells["qty"].Value.ToString();
                    FrmorderDetailUpdateAdd orderDetailUpdateAdd = new FrmorderDetailUpdateAdd(bindingSourceorderDetailUpdate, "新增", orderDetailNo, orderNo, qty);
                    DialogResult result = orderDetailUpdateAdd.ShowDialog();
                    if (result == DialogResult.Yes)
                    {
                        dGridDetail.CurrentRow.Cells["qty"].Value = orderDetailUpdateAdd.retqty;
                    }
                }
                else
                {
                    MessageBox.Show("该订单未审核，无需做进行数量变更");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        private void dGridDetail_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string field;
            field = "pname";
            //string field2;
            //field2 = "colorNum";

            string field3;
            field3 = "unitEn";
            if (e.ColumnIndex == dGridDetail.Columns["pname"].Index)
            {
                Frmprodutselect select = new Frmprodutselect(moduleInfo.productList, field);
                DialogResult result = select.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    dGridDetail.CurrentRow.Cells["pname"].Value = select.pname;
                }
            }

            if (e.ColumnIndex == dGridDetail.Columns["unit"].Index)
            {
                Frmprodutselect select = new Frmprodutselect(moduleInfo.productUnit, field3);
                DialogResult result = select.ShowDialog();
                if (result == DialogResult.Yes)
                {

                    dGridDetail.CurrentRow.Cells["unit"].Value = select.pname;
                }
            }
            //if (e.ColumnIndex == dGridDetail.Columns["colorNum"].Index)
            //{
            //    Frmprodutselect select = new Frmprodutselect(moduleInfo.colorNumList , field2);
            //    DialogResult result = select.ShowDialog();
            //    if (result == DialogResult.Yes)
            //    {

            //        dGridDetail.CurrentRow.Cells["colorNum"].Value = select.pname;
            //    }
            //}
            if (e.ColumnIndex == dGridDetail.Columns["mark"].Index)
            {
                Frmmark mark = new Frmmark();
                DialogResult result = mark.ShowDialog();
                if (result == DialogResult.Yes)
                {

                    dGridDetail.CurrentCell.Value = mark.mark;
                }
            }
            if (e.ColumnIndex == dGridDetail.Columns["waifacust"].Index)
            {
                Frmmark mark = new Frmmark();
                DialogResult result = mark.ShowDialog();
                if (result == DialogResult.Yes)
                {

                    dGridDetail.CurrentCell.Value = mark.mark;
                }
            }
        }

        private void toolStripButton17_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void toolStripButton14_Click(object sender, EventArgs e)
        {
            try
            {
                string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
                string unit = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["unit"].Value.ToString();
                if (basicSql.getOrderStatus(orderNo) == "审核")
                {
                    orderDetailNo = dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString();
                    FrmorderProduceListAdd orderProduceListAdd = new FrmorderProduceListAdd(bindingSourceorderProduceList, "编辑", orderDetailNo, unit);
                    orderProduceListAdd.ShowDialog();
                }
                else
                {
                    MessageBox.Show("该订单未审核，不能编辑生产通知单");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        private void toolStripButton15_Click(object sender, EventArgs e)
        {
            string produceNo = dGridorderProduceList.CurrentRow.Cells["produceNo"].Value.ToString();
            string orderDetailNo = dGridorderProduceList.CurrentRow.Cells["orderDetailNo"].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除生产通知单：" + produceNo + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceorderProduceList.DataSource = orderProduceList.orderProduceListDel(produceNo);
                    dGridorderProduceList.DataSource = bindingSourceorderProduceList;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void toolStripButton16_Click(object sender, EventArgs e)
        {
            //List<string> list = new List<string>();
            //foreach (DataGridViewRow row in dGridorderProduceList.SelectedRows)
            //{
            //    list.Add(row.Cells["produceNo"].Value.ToString());
            //}
            //basicSql.getproduceNo(list);
            //Report r = new Report();
            //DataTable V_ordeProduceList = new DataTable();
            //V_ordeProduceList.Load(basicSql.getproduceNo(list));
            //DataSet ds = new DataSet();
            //ds.Tables.Add(V_ordeProduceList);
            ////  ds.Tables.Add(orderProduceList.getproduceByproduceNo(dGridorderProduceList.CurrentRow.Cells["produceNo"].Value.ToString()));
            //r.Load(@"e:\东华软件项目\报表\orderproduce.frx");
            //r.RegisterData(ds);
            //r.Design();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            bindingSourceorderProduceList.DataSource = orderProduceList.getVorderProduceListByproduceNo("%" + textqury.Text + "%");
            dGridorderProduceList.DataSource = bindingSourceorderProduceList;
            DcolumnSet.colSet(moduleInfo.V_ordeProduceList, dGridorderProduceList);
            toolStripStatusLabel1.Text = "数据总条数:" + dGridorderProduceList.RowCount.ToString() + "条";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            bindingSourceorderProduceList.DataSource = orderProduceList.getVByproduceNo(textorderNo.Text);
            dGridorderProduceList.DataSource = bindingSourceorderProduceList;
            DcolumnSet.colSet(moduleInfo.V_ordeProduceList, dGridorderProduceList);
            toolStripStatusLabel1.Text = "数据总条数:" + dGridorderProduceList.RowCount.ToString() + "条";
        }

        private void dGridDetail_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            //if (dGridDetail.CurrentRow.IsNewRow)
            //{
            //    MessageBox.Show("新行");
            //}
        }

        private void dGridDetail_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            baseSet.datGridSet.gridViesSet.setGridViewRowNum(e, dGridDetail);
            
        }

        private void 复制行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            try
            {
                if (dGridDetail.Rows.Count != 0)
                {
                    
                    orderDataSet.orderDetailDataTable detailTable = (orderDataSet.orderDetailDataTable)bindingSourceorderDetail.DataSource;
                    orderDataSet.orderDetailRow  insertRow = detailTable.NeworderDetailRow ();
                    string  orderDetailNo =dGridDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString();
                    orderDataSet.orderDetailRow crreentRow = detailTable.FindByorderDetailNo(orderDetailNo);
                    insertRow.orderDetailNo = "P" + GlobalInfo.Date + basicSql.getOrderSerail("漂染单号");
                    if (string.IsNullOrEmpty(insertRow.orderDetailNo))
                    {
                      
                       
                        string orderSerial = basicSql.getOrderSerail("漂染单号");
                        basList.basListstretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "漂染单号");
                    }
                   // insertRow.orderDetailNo = "10086";
                    insertRow.pname = crreentRow.pname;
                    insertRow.pcolor = crreentRow.pcolor;
                    insertRow.colorNum = crreentRow.colorNum;
                    insertRow.qty = crreentRow.qty;
                    insertRow.proType = crreentRow.proType;
                    insertRow.ptype = crreentRow.ptype;
                    insertRow.price = crreentRow.price;
                    insertRow.account = crreentRow.account;
                    insertRow.unit = crreentRow.unit;
                    if (!crreentRow.IsmarkNull())
                    {
                        insertRow.mark = crreentRow.mark;
                    }
                    else { insertRow.mark = ""; }
                    insertRow.hetongNo = crreentRow.hetongNo;
                    detailTable.AddorderDetailRow (insertRow);
    
                    bindingSourceorderDetail.MoveLast();
                    bindingSourceorderDetail.EndEdit();

                }
                else
                {
                    MessageBox.Show("不存在需要复制的行");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请选择要复制的行");
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                if (comboBoxcustNum.Text == "")
                {
                    MessageBox.Show("请选择客户编号");
                }
                else
                {
                    try
                    {
                        DialogResult result = MessageBox.Show("是否保存订单表信息！", "提示信息", MessageBoxButtons.YesNo);

                        if (result == DialogResult.Yes)
                        {

                            dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable ptable = (dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable)bindingSourceorderDetail.DataSource;
                            orderMasterRow.orderNo = textorderNo.Text;
                            orderMasterRow.custNum = comboBoxcustNum.SelectedValue.ToString();
                            orderMasterRow.orderDate = dateTimeorderDate.Value;
                            orderMasterRow.receiveDate = dateTimereceiveDate.Value;
                            orderMasterRow.custPoNo = textcustPoNo.Text;
                            orderMasterRow.billNo = comboBoxbizhong.Text;
                            orderMasterRow.orderMark = textmark.Text;
                            orderMasterRow.userNum = comboBoxuserNum.Text;
                            orderMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                            if (textkjf.Text != "")
                            {
                                orderMasterRow.kjf = Convert.ToDecimal(textkjf.Text);
                            }
                            else { orderMasterRow.kjf = 0; }
                            if (textscf.Text != "")
                            {
                                orderMasterRow.scf = Convert.ToDecimal(textscf.Text);
                            }
                            else { orderMasterRow.scf = 0; }
                            orderMasterRow.account = Convert.ToDecimal(textaccount.Text);
                            // orderMasterRow.billNo = "";
                            orderMasterRow.orderMark = textorderMark.Text;
                            orderMasterRow.parkMark = textparkMark.Text;
                            orderMasterRow.orderStatus = "新建";
                            if (comboBoxaddress.SelectedValue == null) orderMasterRow.address = "";
                            else orderMasterRow.address = comboBoxaddress.SelectedValue.ToString();
                            orderMasterRow.createDate = DateTime.Now;
                            orderMasterRow.createUser = GlobalInfo.UserName;
                            orderMasterRow.updateDate = DateTime.Now;
                            orderMasterRow.updateUser = GlobalInfo.UserName;
                            orderMasterRow.mark = textmark.Text;
                            //if (state == "新增")
                            //{
                            bindingSourceorderDetail.AddNew();
                            //}
                            orderMaster.orderMasterInsert(orderMasterRow, ptable, state, GlobalInfo.UserName, textorderNo.Text, GlobalInfo.Date);
                            ptable.AcceptChanges();
                            bindingSourceorderMaster.DataSource = orderMaster.getorderMasterAll();
                            MessageBox.Show("保存成功");
                        }
                    }
                    catch (FormatException)
                    {
                        MessageBox.Show("输入的开机费、金额格式不正确");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
            if (state == "编辑")
            {
                // string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
                if (basicSql.getOrderStatus(textorderNo.Text) == "新建")
                {
                    if (comboBoxcustNum.Text == "")
                    {
                        MessageBox.Show("请选择客户编号");
                    }
                    else
                    {
                        try
                        {
                            DialogResult result = MessageBox.Show("是否保存订单表信息！", "提示信息", MessageBoxButtons.YesNo);

                            if (result == DialogResult.Yes)
                            {

                                dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable ptable = (dhDataAcess.dataAcess.orderDataSet.orderDetailDataTable)bindingSourceorderDetail.DataSource;
                                orderMasterRow.orderNo = textorderNo.Text;
                                orderMasterRow.custNum = comboBoxcustNum.SelectedValue.ToString();
                                orderMasterRow.orderDate = dateTimeorderDate.Value;
                                orderMasterRow.receiveDate = dateTimeorderDate.Value;
                                orderMasterRow.custPoNo = textcustPoNo.Text;
                                // orderMasterRow.billNo = texbillNo.Text;
                                orderMasterRow.orderMark = textmark.Text;
                                orderMasterRow.userNum = comboBoxuserNum.Text;
                                orderMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                                orderMasterRow.kjf = Convert.ToDecimal(textkjf.Text);
                                orderMasterRow.scf = Convert.ToDecimal(textscf.Text);
                                orderMasterRow.account = Convert.ToDecimal(textaccount.Text);
                                orderMasterRow.billNo = "";
                                orderMasterRow.orderMark = textorderMark.Text;
                                orderMasterRow.parkMark = textparkMark.Text;
                                orderMasterRow.orderStatus = "新建";
                              
                                if (comboBoxaddress.SelectedValue == null) orderMasterRow.address = "";
                                else orderMasterRow.address = comboBoxaddress.SelectedValue.ToString();
                                orderMasterRow.createDate = DateTime.Now;
                                orderMasterRow.createUser = GlobalInfo.UserName;
                                orderMasterRow.updateDate = DateTime.Now;
                                orderMasterRow.updateUser = GlobalInfo.UserName;
                                orderMasterRow.mark = textmark.Text;
                                //if (state == "新增")
                                //{
                                bindingSourceorderDetail.AddNew();
                                //}
                                orderMaster.orderMasterInsert(orderMasterRow, ptable, state, GlobalInfo.UserName, textorderNo.Text, GlobalInfo.Date);
                                ptable.AcceptChanges();
                                bindingSourceorderMaster.DataSource = orderMaster.getorderMasterAll();
                                MessageBox.Show("保存成功");
                            }
                        }
                        catch (FormatException)
                        {
                            MessageBox.Show("输入的开机费、金额格式不正确");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                }

                else
                {
                    MessageBox.Show("该订单已审核，不能进行编辑");
                }
            }
        }

        private void comboBoxbizhong_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void 取消整单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string orderDetailNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderDetailNo"].Value.ToString();
                string orderNo = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells["orderNo"].Value.ToString();
                if (basicSql.getOrderStatus(orderNo) == "审核")
                {
                    basicSql.getDetailStatusUpdate(orderNo);
                    basicSql.getorderStatusUpdate(orderNo,"取消");
                    MessageBox.Show("取消订单明细成功");
                }
                else
                {
                    MessageBox.Show("订单没有审核，可直接删除，无需取消");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        private void dGridDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
   
    }
}
